//
//  CWViewStaticTouchEventAnaylzer.m
//  CWDevelop
//
//  Created by LittoCats on 7/15/14.
//  Copyright (c) 2014 Littocats. All rights reserved.
//

#import "CWViewStaticTouchEventAnalyzer.h"
#import "CWBaseView.h"
#import "CWJavaScriptContext.h"

#import "CWBarButtonItem.h"

#import "NSObject+CW.h"

@implementation CWViewStaticTouchEventAnalyzer

+ (instancetype)defaultAnalyzer
{
    static CWViewStaticTouchEventAnalyzer *anaylzer = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        anaylzer = [[CWViewStaticTouchEventAnalyzer alloc] init];
    });
    return anaylzer;
}

+ (void)anaylzeTouchEvents:(NSDictionary *)events ofView:(UIView *)view
{
    if (![events isKindOfClass:[NSDictionary class]] || ![view isKindOfClass:[UIView class]])
        return;
    
    for (NSString *eventType in [events allKeys]) {
        if ([events[eventType] isKindOfClass:[NSDictionary class]])
            [[self defaultAnalyzer] addTouchResponder:events[eventType] toView:view ofType:eventType];
    }
//    [events enumerateObjectsUsingBlock:^(NSDictionary *event, NSUInteger idx, BOOL *stop) {
//        if (![event isKindOfClass:[NSDictionary class]]) return ;
//        [[self defaultAnaylzer] addTouchResponder:event toView:view ofType:event[@"__type"]];
//    }];
}

- (void)addTouchResponder:(NSDictionary *)responder toView:(UIView *)view ofType:(NSString *)type
{
    CWScriptFunction *jsFunction = [CWScriptFunction functionWithFunctionScript:responder[@"__function"] context:responder[@"__context"]];
    if (!jsFunction) return;
    
    if (![view.attributes[@"__CWViewTouchEvents"] isKindOfClass:[NSMutableDictionary class]])
        [view.attributes setObject:[NSMutableDictionary new] forKey:@"__CWViewTouchEvents"];
    [view.attributes[@"__CWViewTouchEvents"] setObject:jsFunction forKey:[NSString stringWithFormat:@"__CWViewTouchEvent_%@",type]];
    if ([view respondsToSelector:@selector(setUserInteractionEnabled:)]) {
        view.userInteractionEnabled = YES;
    }

    UIGestureRecognizer *reco = nil;
    if ([type rangeOfString:@"Click"].location != NSNotFound) {
        NSInteger tapCount = [type rangeOfString:@"Double"].location != NSNotFound ? 2 : 1;
        reco = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToTapGestureRecognizer:)];
        ((UITapGestureRecognizer *)reco).numberOfTapsRequired = tapCount;
        
        [view addGestureRecognizer:reco];
    }
}

- (void)respondsToTapGestureRecognizer:(UITapGestureRecognizer *)reco
{
    UIView *hostview = [reco view];
    NSString *type = reco.numberOfTapsRequired == 1 ? @"onClick" : reco.numberOfTapsRequired == 2 ? @"onDoubleClick" : nil;
    if (!type) return;
    
    [hostview.attributes[@"__CWViewTouchEvents"][[NSString stringWithFormat:@"__CWViewTouchEvent_%@",type]] evaluateWithArguments:hostview._id, nil];
}
#pragma mark- CWControl
- (void)addResponder:(NSDictionary *)responder forEvent:(UIControlEvents)event toControl:(UIControl *)control
{
    CWScriptFunction *jsFunction = [CWScriptFunction functionWithFunctionScript:responder[@"__function"] context:responder[@"__context"]];
    [control.attributes setObject:jsFunction forKey:@"__CWViewTouchEvent_onClick"];
    [control addTarget:self action:@selector(buttonItemClicked:) forControlEvents:event];
}

#pragma mark- CWBarButtonItem
- (void)addTouchResponder:(NSDictionary *)responder toBarButtonItem:(CWBarButtonItem *)item
{
    item.target = self;
    item.action = @selector(buttonItemClicked:);
    if (!item.attributes)
        item.attributes = [NSMutableDictionary new];
    CWScriptFunction *jsFunction = [CWScriptFunction functionWithFunctionScript:responder[@"__function"] context:responder[@"__context"]];
    [item.attributes setObject:jsFunction forKey:@"__CWViewTouchEvent_onClick"];
}
- (void)buttonItemClicked:(CWBarButtonItem *)item
{
    [item.attributes[@"__CWViewTouchEvent_onClick"] evaluateWithArguments:item._id, nil];
}
@end

